package D4;

public class Java03test {
    public static void main(String[] args) {
        int[] array = new int[]{1,2,3,4,5,6,7,8,9};
        System.out.println(binarySearch(array,8));
    }

    public static int binarySearch(int[] arr,int date){
        int left = 0;
        int right = arr.length - 1;
        while (left <= right){
            int middle = (left + right) / 2;
            if (date > arr[middle]){
                left = middle + 1;
            }else if (date < arr[middle]){
                right = middle - 1;
            }else {
                return middle;
            }
        }
        return -1;
    }
}
